/**
 * 
 */
package com.shy.plugin.seo;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;

/**
 * <p>Title:BaiduPing
 * <p>Description:百度ping服务推送
 * @author H2013788
 * @date 2020年11月5日
 */
public class BaiduPing {

    // 百度ping服务接口地址
    private static final String baiduPingApiUrl = "http://ping.baidu.com/ping/RPC2";
    
    public static String post(List<String> urls) {
        int sum = 0;
        try {
            if(null != urls && !urls.isEmpty()) {
                String result = "";
                for (String url : urls) {
                    HttpRequest request = HttpRequest.post(baiduPingApiUrl)
                            .body(getXml(url), "text/xml")
                            .timeout(10000);
                    // request.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.195.217.221", 8089)));
                    result = request.execute().body();
                    if(result.indexOf("<int>0</int>") >= 0) {
                        sum++;
                    }
                }
            }
        } catch (HttpException e) {
            e.printStackTrace();
        }
        return StrUtil.format("成功ping推送{}条记录，失败{}条记录", sum, urls.size() - sum);
    }
    
    
    private static String getXml(String url) {
        StringBuffer xml = new StringBuffer();
        xml.append("<?xml version=\"1.0\"?>");
        xml.append("<methodCall>");
        xml.append("<methodName>weblogUpdates.ping</methodName>");
        xml.append("<params>");
        xml.append("<param>");
        xml.append("<value>");
        xml.append("<string>" + url + "</string>");
        xml.append("</value>");
        xml.append("</param>");
        xml.append("<param>");
        xml.append("<value>");
        xml.append("<string>" + url + "</string>");
        xml.append("</value>");
        xml.append("</param>");
        xml.append("</params>");
        xml.append("</methodCall>");
        return xml.toString();
    }
    
    public static void main(String[] args) {
        List<String> urls = new ArrayList<String>();
        urls.add("https://www.aslunwen.net/zxxs/48233.html");
        urls.add("https://www.aslunwen.net/rmxs/48232.html");
        urls.add("https://www.aslunwen.net/jxxs/48231.html");
        System.out.println(post(urls));
    }
    
    
}
